package com.health.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.dao.MemberMapper;
import com.health.pojo.Member;
import com.health.vo.PageResult;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class MemberServiceImpl implements MemberService{

    @Resource
    private MemberMapper memberMapper;
    /**
     * 获取会员id
     */
    public int findMemberId(Member member) {
        //根据手机号查询会员id
        Integer memberId=memberMapper.findMemberId(member.getPhoneNumber());

        if (memberId==null){
            //没有该会员,添加新的会员
            memberMapper.add(member);
            //获取新加的会员id
             memberId = member.getId();
        }
        return memberId;
    }
    /**
     * 查询会员数据报告
     */
    public Map<String, Object> findMemberReport() {
        return memberMapper.findMemberReport();
    }

    /**
     * 查询会员数量
     */
    public List<Integer> findCountByDate(String firstDay, int[] arr) {
        return memberMapper.findCountByDate(firstDay,arr);
    }

    /**
     * 会员表分页查询
     */
    public PageResult<Member> findPage(Integer pageNow, Integer pageSize, String query) {
        //引入分页插件
        PageHelper.startPage(pageNow,pageSize);

        //查询
       Page<Member> page= memberMapper.findByQuery(query);

        return new PageResult<Member>(page.getTotal(),page.getResult());
    }

    /**
     * 删除会员
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Integer id) {
        //查询order表,有被使用则无法删除
        int count=memberMapper.findCountInOrder(id);
        //判断
        if (count>0){
            throw new RuntimeException("该会员已有预约,无法删除");
        }
        return memberMapper.deleteById(id);
    }
    /**
     * 添加会员
     */
    public int add(Member member) {
        return memberMapper.addMember(member);
    }
    /**
     * 编辑会员-根据id查询会员数据回显
     */
    public Member findById(Integer id) {
        return memberMapper.findById(id);
    }
    /**
     * 编辑会员
     */
    public int update(Member member) {
        return memberMapper.update(member);
    }
}
